package handler;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
//import java.io.*;
//import java.util.*;
import structures.User;
//import structures.Tag;

/**
 * ImageHandler
 * @author Jose Antonio Prieto
 */
public class UserHandler extends DefaultHandler {

    /** User */
    private User user;
    /** Accumulator */
    private StringBuffer accumulator;

    /**
     * Constructor
     */
    public UserHandler() {
        user = new User();
    }

    /**
     * Constructor
     * @param ph
     */
    public UserHandler(User ph) {
        user = ph;
    }

    /**
     * Called at the beginning
     */
    public void startDocument() {
        accumulator = new StringBuffer();
    }

    /**
     * When the parser encounters plain text (not XML elements), it calls this method, which accumulates 
     * the text in a string buffer. Note that this method may be called in multiple times.
     * @param buffer
     * @param start
     * @param length
     */
    public void characters(char[] buffer, int start, int length) {
        accumulator.append(buffer, start, length);
    }

    /**
     * At the beginning of each new element, erase any accumulated text and capture attributes
     * @param namespaceURL
     * @param localName
     * @param qname
     * @param attributes
     */
    public void startElement(String namespaceURL, String localName, String qname, Attributes attributes) {
        accumulator.setLength(0);
        if (localName.equals("image")) {
            user.setId(attributes.getValue("id"));
        } 
    }

    /**
     * Take special action when reach the end of selected elements. 
     * In this case the accumulated text is stored in the image object
     * @param namespaceURL
     * @param localName
     * @param qname
     */
    public void endElement(String namespaceURL, String localName, String qname) {
        
        if (localName.equals("id")) {
            user.setId(accumulator.toString().trim());
        }
        if (localName.equals("firstName")) {
            user.setId(accumulator.toString().trim());
        } 
        if (localName.equals("lastName")) {
            user.setId(accumulator.toString().trim());
        } 
        if (localName.equals("nickName")) {
            user.setId(accumulator.toString().trim());
        } 
        if (localName.equals("userDirectory")) {
            user.setId(accumulator.toString().trim());
        } 
        if (localName.equals("email")) {
            user.setId(accumulator.toString().trim());
        } 
    }

    /**
     * Called at the end
     */
    public void endDocument() {
    }

    /**
     * Warning
     * @param exception
     */
    public void warning(SAXParseException exception) {
        System.err.println("WARNING: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
    }

    /**
     * Error
     * @param exception
     */
    public void error(SAXParseException exception) {
        System.err.println("ERROR: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
    }

    /**
     * Fatal error
     * @param exception
     * @throws org.xml.sax.SAXException
     */
    public void fatalError(SAXParseException exception) throws SAXException {
        System.err.println("FATAL: line " + exception.getLineNumber() + ": " +
                exception.getMessage());
        throw (exception);
    }
}
